using System;
using System.Drawing;
using System.Data;
using System.Linq;
using FarPoint.Win.Spread;
using System.Reflection;
using FarPoint.Win.Spread.DrawingSpace;
using System.Text;

namespace Spread.FloatingObjects.Features
{
    public partial class ShapeActions : Spread.Common.Features.Sample.BaseSampleControl
    {
        public ShapeActions()
        {
            InitializeComponent();
            Name = "Shape Actions";

            InitSpreadSheet(fpSpread.ActiveSheet);
        }

        private static string ConvertToHexString(byte[] bytes)
        {
            StringBuilder hex = new StringBuilder();
            foreach (byte b in bytes)
            {
                string hexPair = Convert.ToString(b, 16);
                if (hexPair.Length < 2)
                {
                    hexPair = "0" + hexPair;
                }
                hex.Append(hexPair);
            }
            return hex.ToString();
        }


        /// <summary>
        /// Create new instance of FarPoint.Win.Spread.DrawingSpace.<ShapeName>
        /// </summary>
        /// <param name="shapeName"></param>
        /// <returns></returns>
        internal static PSShape CreateShapeInstance(string shapeName)
        {
            Assembly currentAssembly = typeof(FpSpread).Assembly;
            AssemblyName assemblyName = currentAssembly.GetName();
            string fullname = currentAssembly.FullName;

            string spAssemblyShortName = "FarPoint.Win.Spread";
            string spAssemblyVersion = assemblyName.Version.ToString();
            string spAssemblyCulture = (fullname.Split(','))[2].Substring(9);
            byte[] spAssemblyPublicKeyTokenBytes = assemblyName.GetPublicKeyToken();
            string spAssemblyPublicKeyToken = ConvertToHexString(spAssemblyPublicKeyTokenBytes);
            string spNameTemplate = "{0}, Version={1}, Culture={2}, PublicKeyToken={3}";

            string spAssemblyFullName = string.Format(spNameTemplate,
                                                spAssemblyShortName,
                                                spAssemblyVersion,
                                                spAssemblyCulture,
                                                spAssemblyPublicKeyToken);
            return Activator.CreateInstance(spAssemblyFullName, "FarPoint.Win.Spread.DrawingSpace." + shapeName)
                    .Unwrap() as PSShape;
        }

        private void InitSpreadSheet(SheetView sheet)
        {           
            var shapes = Assembly.GetAssembly(typeof(PSShape)).GetTypes().Where(type => type.IsSubclassOf(typeof(PSShape)) &&
                !type.IsAbstract && type.Name.Contains("Shape")).Take(12).ToList();

            const int WIDTH = 100, HEIGHT = 100;
            int row = 3, col = 2;
            
            for (int i = 0; i < shapes.Count; ++i)
            {   
                PSShape shape = CreateShapeInstance(shapes[i].Name);
                shape.Size = new Size(WIDTH, HEIGHT);
                sheet.AddShape(shape, row, col);

                col += 5;
                if (col > 12)
                {
                    row += 10;
                    col = 2;
                }
            }
        }
    }
}


//
//ShapeActions.Designer.cs
//


namespace Spread.FloatingObjects.Features
{
    partial class ShapeActions
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White;
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // ShapeActions
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "ShapeActions";
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
    }
}
